import 'package:t3_data_service/biz_data_source/map/address/city_pick/model/city_pick_models.dart';
import 'package:t3_data_service/biz_data_source/map/address/poi_search/poi_search.dart';
import 'package:t3_data_service/biz_data_source/map/poi/lat_lng.dart';
import 'package:t3_data_service/data_provider/map_sctx/t3_map_sctx_provider.dart';
import 'address_select_enums.dart';
import 'poi_search_base_config.dart';
import 'package:t3_data_service/data_provider/live_location/t3_live_location_provider.dart';
import 'package:t3_foundation/t3_foundation.dart';

class POISearchMultiPointsConfig extends POISearchBaseConfig {
  /// 多点选择：是否清空光标所在输入框文字
  bool clearInputText = false;
  /// 终点是否是机场，如果是机场则内部走机场交互逻辑
  bool? isEndPoiAirport;
  /// 忽略初始化赋值的地址：修改途经点和终点时为true
  bool ignoreLocation = false;
  /// 光标位置: 途经点场景有用
  CursorLocation cursorLocation = CursorLocation.endPoi;
  /// 是否进入图选：图选进入的页面为false
  bool showConfirmEndPointTag = true;
  /// 终点POI
  POIModel? endPoint;
  /// 途径点
  List<POIModel>? viaPoints;

  List<POIModel>? get initPoints {
    var result = <POIModel>[];
    for (var item in viaPoints ?? []) {
      result.add(item);
    }
    if (endPoint != null) {
      result.add(endPoint!);
    }
    if (result.isEmpty) {
      return null;
    }
    return result;
  }

  /// 终点是机场
  bool get isAirportEndPoint {
    if (endPoint != null && isEndPoiAirport == true) {
      return true;
    }
    return false;
  }

  POISearchMultiPointsConfig({
    required super.location,
    super.originCoor,
    super.sceneRuleId,
    super.refererPageTitle,
    super.allowMoveOutFence = true,
    super.pointType = T3POIType.start,
    super.isUserActionDisabledForEnd = false,
    super.isUserActionDisabledForVia = false,
    super.needHideAddMidwayButton = false,
    super.style = T3POIStyle.normal,
    super.cityScope = CityScope.opened,
    super.showMapSelectedFooter = false,
    super.showConfusionButton = true,
    super.showBackButton = false,
    super.shouldCheckTableSelectedPoi = true,
    super.entryOptions = const [],
    super.autoOpenCitySelection = false,
    super.popUntilRoute,
    super.pageScene,
    this.clearInputText = false,
    this.isEndPoiAirport,
    this.cursorLocation = CursorLocation.endPoi,
    this.endPoint,
    this.viaPoints,
    this.showConfirmEndPointTag = true,
  });

  static POISearchMultiPointsConfig fromJson(Map<dynamic, dynamic> json) {
    List<POIModel>? vias;
    var viaPointsList = json['viaPoints'];
    if (viaPointsList is List<dynamic>?) {
      vias = viaPointsList?.map((e) => POIModel.fromJson(e)).toList();
    }

    POIModel? endPoint;
    if (json['endPoint'] != null && json['endPoint'] is Map) {
      endPoint = POIModel.fromJson(json['endPoint']);
    }
    var config = POISearchMultiPointsConfig(
      location: $T3POILocationDecode(json['location']),
      originCoor: LatLng.fromJson(json['originCoor']),
      allowMoveOutFence: BoolUtils.dynamicToBool(json['allowMoveOutFence'], true),
      pointType: T3POIType.fromJson(IntUtils.dynamicToNullInt(json['pointType'])),
      isUserActionDisabledForEnd: BoolUtils.dynamicToBool(json['isUserActionDisabledForEnd']),
      isUserActionDisabledForVia: BoolUtils.dynamicToBool(json['isUserActionDisabledForVia']),
      needHideAddMidwayButton: BoolUtils.dynamicToBool(json['needHideAddMidwayButton']),
      style: T3POIStyle.fromJson(IntUtils.dynamicToNullInt(json['style'])),
      cityScope: CityScopeExtension.initWithRawValue(IntUtils.dynamicToNullInt(json['cityScope'])) ?? CityScope.opened,
      showMapSelectedFooter: BoolUtils.dynamicToBool(json['showMapSelectedFooter']),
      showConfusionButton: BoolUtils.dynamicToBool(json['showConfusionButton'],true),
      showBackButton: BoolUtils.dynamicToBool(json['showBackButton']),
      sceneRuleId: StringUtils.dynamicToString(json['sceneRuleId']),
      refererPageTitle: StringUtils.dynamicToString(json['refererPageTitle']),
      shouldCheckTableSelectedPoi: BoolUtils.dynamicToBool(json['shouldCheckTableSelectedPoi'], true),
      entryOptions: $T3EntryOptionsEnumDecode(IntUtils.dynamicToNullInt(json['entryOptions'])),
      autoOpenCitySelection: BoolUtils.dynamicToBool(json['autoOpenCitySelection'], false),
      popUntilRoute: StringUtils.dynamicToString(json['popUntilRoute']),
      pageScene: PageScene.fromJson(json['pageScene']),
      clearInputText: BoolUtils.dynamicToBool(json['clearInputText'], false),
      isEndPoiAirport: BoolUtils.dynamicToNullBool(json['isEndPoiAirport']),
      cursorLocation: CursorLocation.fromJson(StringUtils.dynamicToString(json['cursorLocation'])),
      endPoint: endPoint,
      viaPoints: vias,
      showConfirmEndPointTag: BoolUtils.dynamicToBool(json['showConfirmEndPointTag'],true),
    );
    config.mapType = T3MapType.fromJson(IntUtils.dynamicToNullInt(json['mapType']));
    return config;
  }

  @override
  Map<String, dynamic> toJson() {
    var json = super.toJson();
    var newJson = {
      'clearInputText': clearInputText,
      'isEndPoiAirport': isEndPoiAirport,
      'cursorLocation': cursorLocation.name,
      'endPoint' : endPoint?.toJson(),
      'showConfirmEndPointTag': showConfirmEndPointTag,
      'viaPoints' : viaPoints?.map((e) => e.toJson()).toList()
    };
    for (var item in newJson.entries) {
      json[item.key] = item.value;
    }
    return json;
  }

  factory POISearchMultiPointsConfig.endNormal(
      {required T3POILocation location, LatLng? originCoor}) {
    /// 如果经纬度有问题使用大头针位置
    var inLocation = location;
    if (location.coordinate.lat == 0 || location.coordinate.lng == 0) {
      var pin = T3LiveLocationProvider.instance.pinLocation;
      if (pin != null) {
        if (pin.adCode != null && pin.city != null && pin.latitude != 0 && pin.longitude != 0) {
          var coor = LatLng(pin.latitude, pin.longitude);
          inLocation = T3POILocation(adCode: pin.adCode ?? '', cityName: pin.city ?? '', coordinate: coor);
        }
      }
    }
    return POISearchMultiPointsConfig(
      location: inLocation,
      originCoor: originCoor,
      pointType: T3POIType.end,
      showMapSelectedFooter: true,
      entryOptions: [T3EntryOptions.homeAndCompany, T3EntryOptions.map],
    );
  }
}